/**
 * 提报详情
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('ProjectSettleCtrl', ['$scope', '$http', 'MHService',
      function ($scope, $http, MHService) {
        var data = MHService.globalData(false, {key: 'materialSubmitData'}),
          gardenid = parseInt(MHService.globalData(false, {key: 'projectId'})),//项目id
          proid = parseInt(MHService.globalData(false, {key: 'procedureid'})),//流程id
          modeid = parseInt(MHService.globalData(false, {key: 'modeid'}));//模块id

        $scope.projects = data;
        /**
         * 将数据提交到服务器
         */
        $scope.submit = function () {
          var datacost = [], datavalue = [], zwseedsArray = [];
          var _req = new FormData();//新建一个表单对象，填入固定的数据
          _req.append('gardenid', gardenid);
          _req.append('proid', proid);
          _req.append('modeid', modeid);

          for (var index in data) {
            _req.append('datacost', data[index].id);//计划成本id
            _req.append('datavalue', data[index].actual);//实际填报数量
            //如果是精品植物添加苗木数据
            if (data[index].classid === '精品植物') {
              var _item = [], _data = data[index];
              _item.push(_data.id);
              _item.push(_data.died);
              _item.push(_data.dying);
              _item.push(_data.add);
              _item.push(_data.added);
              _item.push(_data.materialid);
              //zwseedsArray.push(_item.join(','));
              _req.append('zwseedsArray', _item.join(','));
            }
          }
          $.ajax({
            type: 'POST',
            url: MHService.URL.material_submit,
            data: _req,
            processData: false,
            contentType: false,
            complete: function (data) {
              //var _data = JSON.parse(data.responseText);
              console.log(data.responseText);
              if (data.responseText.match('"success":true')) {
                MHService.Toast('提交成功');
                history.go(-1);
              } else {
                MHService.Toast('提交失败，请刷新重试');
              }
            }
          });
        }
      }]);
  };
});
